package net.kem.android.positioner;

import net.kem.android.positioner.R;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * This example taken from
 * http://www.codeproject.com/Articles/112044/positioner-
 * App-to-Find-Current-Nearest-Location-us
 *
 * @author Evgeny.Kurtser
 *
 */
public class SettingsActivity extends Activity {
	private EditText editHost;
	private EditText editPort;
	private EditText editUsername;
	private EditText editPassword;

	private EditText editGPSRefreshInterval;
	private EditText editGPSRefreshDistance;
	private EditText editPOIRadius;

	private Button buSave;
	private Button buCancel;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		Settings.init(this);

		editHost = (EditText) findViewById(R.id.settingsHost);
		editPort = (EditText) findViewById(R.id.settingsPort);
		editUsername = (EditText) findViewById(R.id.settingsUsername);
		editPassword = (EditText) findViewById(R.id.settingsPassword);
		editGPSRefreshInterval = (EditText) findViewById(R.id.settingsGPSRefreshInterval);
		editGPSRefreshDistance = (EditText) findViewById(R.id.settingsGPSRefreshDistance);
		editPOIRadius = (EditText) findViewById(R.id.settingsPOIRadius);
		buSave = (Button) findViewById(R.id.settingsButtonSave);
		buCancel = (Button) findViewById(R.id.settingsButtonCancel);

		// Restore preferences
		editHost.setText(Settings.getHost());
		editPort.setText(String.valueOf(Settings.getPort()));
		editUsername.setText(Settings.getUsername());
		editPassword.setText(Settings.getPassword());
		editGPSRefreshInterval.setText(String.valueOf(Settings.getGPSRefreshInterval()));
		editGPSRefreshDistance.setText(String.valueOf(Settings.getGPSRefreshDistance()));
		editPOIRadius.setText(String.valueOf(Settings.getPOIRadius()));


//		Editor e = this.getPreferences(Context.MODE_PRIVATE).edit();
//		e.putString("password", mPassword);
//		e.commit();

		editGPSRefreshInterval.addTextChangedListener(new TextWatcher() {
			@Override public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			@Override public void afterTextChanged(Editable s) {
				if(s.toString().length() == 0) {
					String errorMsg = getString(R.string.errorCannotBeEmpty);
					editGPSRefreshInterval.setError(errorMsg);
					buSave.setEnabled(false);
				} else {
					buSave.setEnabled(true);
				}
			}
		});
		editGPSRefreshDistance.addTextChangedListener(new TextWatcher() {
			@Override public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			@Override public void afterTextChanged(Editable s) {
				if(s.toString().length() == 0) {
					String errorMsg = getString(R.string.errorCannotBeEmpty);
					editGPSRefreshDistance.setError(errorMsg);
					buSave.setEnabled(false);
				} else {
					buSave.setEnabled(true);
				}
			}
		});
		editPOIRadius.addTextChangedListener(new TextWatcher() {
			@Override public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			@Override public void afterTextChanged(Editable s) {
				if(s.toString().length() == 0) {
					String errorMsg = getString(R.string.errorCannotBeEmpty);
					editPOIRadius.setError(errorMsg);
					buSave.setEnabled(false);
				} else {
					buSave.setEnabled(true);
				}
			}
		});

		buSave.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				// Save user preferences. We need an Editor object to make changes.
				Settings.setHost(editHost.getText().toString());
				Settings.setPort(Integer.parseInt(editPort.getText().toString()));
				Settings.setUsername(editUsername.getText().toString());
				Settings.setPassword(editPassword.getText().toString());
				Settings.setGPSRefreshInterval(Integer.parseInt(editGPSRefreshInterval.getText().toString()));
				Settings.setGPSRefreshDistance(Integer.parseInt(editGPSRefreshDistance.getText().toString()));
				Settings.setPOIRadius(Integer.parseInt(editPOIRadius.getText().toString()));
				setResult(RESULT_OK);
				finish();
			}
		});
		buCancel.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				//SettingsActivity.this.setVisible(false);
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}
}